package com.tang.trap.receiver;

import java.io.IOException;
import java.net.UnknownHostException;

import com.tang.constants.UserConstants;
import lombok.extern.slf4j.Slf4j;
import org.snmp4j.CommandResponder;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.*;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;

/**
 * @author tangfl
 */
@Slf4j
public class TrapReceiver {	
	
    private MultiThreadedMessageDispatcher dispatcher;
    private Snmp snmp = null;
    private Address listenAddress;
    private ThreadPool threadPool;

    public TrapReceiver(CommandResponder listener)  throws UnknownHostException, IOException {
    	 threadPool = ThreadPool.create("Trap", 2);
         dispatcher = new MultiThreadedMessageDispatcher(threadPool,
                 new MessageDispatcherImpl());
         log.debug("监听的IP和端口是={}",System.getProperty(
                 "snmp4j.listenAddress", "udp:127.0.0.1/162"));
         listenAddress = GenericAddress.parse(System.getProperty(
                 "snmp4j.listenAddress", "udp:127.0.0.1/162")); // 本地IP与监听端口
//         listenAddress = GenericAddress.parse(address); // 本地IP与监听端口
         TransportMapping<?> transport;
         // 对TCP与UDP协议进行处理
         if (listenAddress instanceof UdpAddress) {
             transport = new DefaultUdpTransportMapping(
                     (UdpAddress) listenAddress);
         } else {
             transport = new DefaultTcpTransportMapping(
                     (TcpAddress) listenAddress);
         }
         snmp = new Snmp(dispatcher, transport);
         snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());
         snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());
         snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3());
         USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3
                 .createLocalEngineID()), 0);
         SecurityModels.getInstance().addSecurityModel(usm);
		 snmp.getUSM().addUser(new OctetString(UserConstants.USERNAME), new UsmUser(new OctetString(UserConstants.USERNAME),
				AuthSHA.ID, new OctetString(UserConstants.AUTH_PASSWORD), PrivAES128.ID, new OctetString(UserConstants.PRIV_PASSWORD)));
         snmp.listen();
         log.debug("开始监听={}");
         snmp.addCommandResponder(listener);
    }

	public MultiThreadedMessageDispatcher getDispatcher() {
		return dispatcher;
	}

	public void setDispatcher(MultiThreadedMessageDispatcher dispatcher) {
		this.dispatcher = dispatcher;
	}

	public Snmp getSnmp() {
		return snmp;
	}

	public void setSnmp(Snmp snmp) {
		this.snmp = snmp;
	}

	public Address getListenAddress() {
		return listenAddress;
	}

	public void setListenAddress(Address listenAddress) {
		this.listenAddress = listenAddress;
	}

	public ThreadPool getThreadPool() {
		return threadPool;
	}

	public void setThreadPool(ThreadPool threadPool) {
		this.threadPool = threadPool;
	}
    
}

